<?php
/**
 * Created by PhpStorm.
 * User: Donal
 * Date: 16/4/25
 * Time: 下午5:24
 */

namespace Home\Lib;

define('HOST','http://sdk.open.api.igexin.com/apiex.htm');
define('APPKEY','bStLfBKQ1Y6IK54ebUvMMA');
define('APPID','JF5g0qW7Xd9VpX9Zd3SXi9');
define('MASTERSECRET','qaC1M13IAE8pHsWuo1px31');

define('GETUI_PATH', APP_REAL_PATH. '/Home/Getui/');
require_once(GETUI_PATH. '/IGt.Push.php');
require_once(GETUI_PATH. '/igetui/IGt.AppMessage.php');
require_once(GETUI_PATH. '/igetui/IGt.APNPayload.php');
require_once(GETUI_PATH. '/igetui/template/IGt.BaseTemplate.php');
require_once(GETUI_PATH. '/IGt.Batch.php');
require_once(GETUI_PATH. '/igetui/utils/AppConditions.php');

class PushManager {

    public function pushMessageToApp(){

        $igt = new \IGeTui(HOST,APPKEY,MASTERSECRET);
        //定义透传模板，设置透传内容，和收到消息是否立即启动启用
        $template = $this->IGtNotificationTemplateDemo();
        //$template = IGtLinkTemplateDemo();
        // 定义"AppMessage"类型消息对象，设置消息内容模板、发送的目标App列表、是否支持离线发送、以及离线消息有效期(单位毫秒)
        $message = new \IGtAppMessage();
        $message->set_isOffline(true);
        $message->set_offlineExpireTime(10 * 60 * 1000);//离线时间单位为毫秒，例，两个小时离线为3600*1000*2
        $message->set_data($template);

        $appIdList=array(APPID);
//        $phoneTypeList=array('ANDROID');
//        $provinceList=array('浙江');
//        $tagList=array('haha');
        //用户属性
        //$age = array("0000", "0010");


        //$cdt = new AppConditions();
        // $cdt->addCondition(AppConditions::PHONE_TYPE, $phoneTypeList);
        // $cdt->addCondition(AppConditions::REGION, $provinceList);
        //$cdt->addCondition(AppConditions::TAG, $tagList);
        //$cdt->addCondition("age", $age);

        $message->set_appIdList($appIdList);
        //$message->set_conditions($cdt->getCondition());

        $rep = $igt->pushMessageToApp($message,"任务组名");

        var_dump($rep);
        echo ("<br><br>");
    }

    function pushMessageToCliendId($cid){
        $igt = new \IGeTui(HOST,APPKEY,MASTERSECRET);

        //消息模版：
        // 4.NotyPopLoadTemplate：通知弹框下载功能模板
        $template = $this->IGtNotificationTemplateDemo();


        //定义"SingleMessage"
        $message = new \IGtSingleMessage();

        $message->set_isOffline(true);//是否离线
        $message->set_offlineExpireTime(3600*12*1000);//离线时间
        $message->set_data($template);//设置推送消息类型
        //$message->set_PushNetWorkType(0);//设置是否根据WIFI推送消息，2为4G/3G/2G，1为wifi推送，0为不限制推送
        //接收方
        $target = new \IGtTarget();
        $target->set_appId(APPID);
        $target->set_clientId($cid);
//    $target->set_alias(Alias);

        try {
            $rep = $igt->pushMessageToSingle($message, $target);
//            var_dump($rep);
//            echo ("<br><br>");

        }catch(\RequestException $e){
            $requstId =$e->getRequestId();
            //失败时重发
            $rep = $igt->pushMessageToSingle($message, $target,$requstId);
//            var_dump($rep);
//            echo ("<br><br>");
        }
    }

    function pushMessageToUid($uid, $template){
        $igt = new \IGeTui(HOST,APPKEY,MASTERSECRET);

        $GePush = D('IMGetuiPush');
        $result = $GePush->where('uid=%d', $uid)->find();
        if ($result) {
            $cid = $result['client_id'];
            //定义"SingleMessage"
            $message = new \IGtSingleMessage();
            $message->set_isOffline(true);//是否离线
            $message->set_offlineExpireTime(3600*12*1000);//离线时间
            $message->set_data($template);//设置推送消息类型
            $message->set_PushNetWorkType(0);//设置是否根据WIFI推送消息，2为4G/3G/2G，1为wifi推送，0为不限制推送
            //接收方
            $target = new \IGtTarget();
            $target->set_appId(APPID);
            $target->set_clientId($cid);
            try {
                $rep = $igt->pushMessageToSingle($message, $target);
//                var_dump($rep);
//                echo ("<br><br>");

            }catch(\RequestException $e){
                $requstId =$e->getRequestId();
                //失败时重发
                $rep = $igt->pushMessageToSingle($message, $target,$requstId);
//                var_dump($cid);
//                echo ("<br><br>");
            }
        }
//        else {
//            $data['msg'] = 'no cid';
//            $this->ajaxReturn($data);
//        }

    }

    function pushTransimisionMessageToUid($uid, $template){
        $igt = new \IGeTui(HOST,APPKEY,MASTERSECRET);

        $GePush = D('IMGetuiPush');
        $result = $GePush->where('uid=%d', $uid)->find();
        if ($result) {
            $cid = $result['client_id'];
            //定义"SingleMessage"
            $message = new \IGtSingleMessage();
            $message->set_isOffline(true);//是否离线
            $message->set_offlineExpireTime(3600*12*1000);//离线时间
            $message->set_data($template);//设置推送消息类型
            $message->set_PushNetWorkType(0);//设置是否根据WIFI推送消息，2为4G/3G/2G，1为wifi推送，0为不限制推送
            //接收方
            $target = new \IGtTarget();
            $target->set_appId(APPID);
            $target->set_clientId($cid);
            try {
                $rep = $igt->pushMessageToSingle($message, $target);
//                var_dump($rep);
//                echo ("<br><br>");

            }catch(\RequestException $e){
                $requstId =$e->getRequestId();
                //失败时重发
                $rep = $igt->pushMessageToSingle($message, $target,$requstId);
//                var_dump($rep);
//                echo ("<br><br>");
            }
        }
    }

    function IGtNotificationTemplateApplyFriend(){
        $template =  new \IGtNotificationTemplate();
        $template->set_appId(APPID);                   //应用appid
        $template->set_appkey(APPKEY);                 //应用appkey
        $template->set_transmissionType(1);            //透传消息类型
        $template->set_transmissionContent("测试离线");//透传内容
        $template->set_title("DTalk");                  //通知栏标题
        $template->set_text("你有一个新朋友想要加你,赶快去看看");     //通知栏内容
        $template->set_logo("");                       //通知栏logo
        $template->set_logoURL("");                    //通知栏logo链接
        $template->set_isRing(true);                   //是否响铃
        $template->set_isVibrate(true);                //是否震动
        $template->set_isClearable(true);              //通知栏是否可清除

        return $template;
    }

    function IGtNotificationTemplateConfirmFriend(){
        $template =  new \IGtNotificationTemplate();
        $template->set_appId(APPID);                   //应用appid
        $template->set_appkey(APPKEY);                 //应用appkey
        $template->set_transmissionType(1);            //透传消息类型
        $template->set_transmissionContent("测试离线");//透传内容
        $template->set_title("DTalk");                  //通知栏标题
        $template->set_text("你有一个新朋友加你了,赶快去聊聊");     //通知栏内容
        $template->set_logo("");                       //通知栏logo
        $template->set_logoURL("");                    //通知栏logo链接
        $template->set_isRing(true);                   //是否响铃
        $template->set_isVibrate(true);                //是否震动
        $template->set_isClearable(true);              //通知栏是否可清除

        return $template;
    }

    function IGtTransmissionTemplateApplyFriend($data){
        $template =  new \IGtTransmissionTemplate();
        $template->set_appId(APPID);
        $template->set_appkey(APPKEY);
        $template->set_transmissionType(2);
        $template->set_transmissionContent($data);//透传内容
        //$template->set_duration(BEGINTIME,ENDTIME); //设置ANDROID客户端在此时间区间内展示消息
        //APN简单推送
//        $template = new IGtAPNTemplate();
//        $apn = new IGtAPNPayload();
//        $alertmsg=new SimpleAlertMsg();
//        $alertmsg->alertMsg="";
//        $apn->alertMsg=$alertmsg;
////        $apn->badge=2;
////        $apn->sound="";
//        $apn->add_customMsg("payload","payload");
//        $apn->contentAvailable=1;
//        $apn->category="ACTIONABLE";
//        $template->set_apnInfo($apn);
//        $message = new IGtSingleMessage();

        //APN高级推送
        $apn = new \IGtAPNPayload();
        $alertmsg=new \DictionaryAlertMsg();
        $alertmsg->body="body";
        $alertmsg->actionLocKey="ActionLockey";
        $alertmsg->locKey="LocKey";
        $alertmsg->locArgs=array("locargs");
        $alertmsg->launchImage="launchimage";
//        IOS8.2 支持
        $alertmsg->title="Title";
        $alertmsg->titleLocKey="TitleLocKey";
        $alertmsg->titleLocArgs=array("TitleLocArg");

        $apn->alertMsg=$alertmsg;
        $apn->badge=7;
        $apn->sound="";
        $apn->add_customMsg("payload","payload");
        $apn->contentAvailable=1;
        $apn->category="ACTIONABLE";
        $template->set_apnInfo($apn);

        //PushApn老方式传参
//    $template = new IGtAPNTemplate();
//          $template->set_pushInfo("", 10, "", "com.gexin.ios.silence", "", "", "", "");

        return $template;
    }
}